package qx.hyj.petrescue.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import qx.hyj.petrescue.VO.BlogVo;
import qx.hyj.petrescue.VO.CommentVo;
import qx.hyj.petrescue.entity.Blog;
import qx.hyj.petrescue.entity.Comment;
import qx.hyj.petrescue.entity.Liked;

import java.util.List;

@Repository
public interface Blogmapper {
    @Insert("insert into liked (luser,lblog) select #{luser},#{lblog} from dual " +
            "where not exists(select 1 from liked where luser = #{luser} and lblog = #{lblog})")
        //  使用dual
    Integer addlike(Liked liked);
    @Insert("insert into blog (author, blogtype, title, content, picture, dwtype) " +
            "values (#{author}, #{blogtype}, #{title}, #{content}, #{picture}, #{dwtype})")
    Integer addBlog(Blog blog);
    @Select("select * from blog " +
            "order by id asc " +
            "limit #{limit}")
    List<BlogVo> getOldestBlogs(int limit);

    @Select("SELECT * FROM blog WHERE blogtype = #{blogType} AND dwtype = #{dwType} ORDER BY readed DESC")
    List<BlogVo> getTypeBlogs(@Param("blogType")Integer blogType,@Param("dwType") Integer dwType);

    @Insert("insert into comment (cblog,cuser,comment) " +
            "values (#{cblog}, #{cuser}, #{comment})")
    Integer addComment(Comment comment);

    @Select("select * from comment " +
            "where cblog = #{cblog} " +
            "order by id asc")
    List<CommentVo> getComments(Integer cblog);

    @Update("UPDATE blog SET readed = readed + 1 WHERE id = #{id}")
    Integer updateRead(Integer id);
    Blog searchByColumn (Blog blog);

    @Select("SELECT * FROM blog WHERE title LIKE CONCAT('%', #{search}, '%') OR content LIKE CONCAT('%', #{search}, '%')")
    List<BlogVo> searchBlogsBysearch(String search);

    @Select("SELECT * FROM blog WHERE  author = #{author}")
    List<BlogVo> searchBlogsByAuthor(Integer author);

}
